package com.practicesuite.controllayer;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JTextField;

import com.practicesuite.systemlayer.Doctor;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Panel for filling out information to create a new appointment in the system.
 * 
 * @author James
 *
 */
public class RequestAppointmentPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    public MainWindow window;
    private GregorianCalendar calendar;
    private int yearNow;
    private String[] years;
    private List<Doctor> doctors;
    String[] doctorNames;
    private final Patient patient;
    private JTextField textField;

    /**
     * Create the panel.
     */
    public RequestAppointmentPanel(MainWindow container, final User user) {
        this.window = container;
        patient = (Patient)user;
        
        try {
            doctors = SystemDriver.getDoctorList();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        doctorNames = new String[doctors.size() + 1];
        doctorNames[0] = "Select";
        for (int i = 1; i <= doctors.size(); i++) {
            doctorNames[i] = doctors.get(i - 1).getName();
        }
        
        calendar = new GregorianCalendar();
        yearNow = calendar.get(GregorianCalendar.YEAR);
        years = new String[] {"Year", Integer.toString(yearNow - 1), Integer.toString(yearNow), 
                Integer.toString(yearNow + 1), Integer.toString(yearNow + 2), Integer.toString(yearNow + 3)};
        
        setPreferredSize(new Dimension(500, 400));
        setLayout(new GridLayout(0, 1, 0, 0));
        
        JLabel lblRequestAppointment = new JLabel("Request Appointment");
        lblRequestAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblRequestAppointment.setFont(new Font("Tahoma", Font.PLAIN, 24));
        add(lblRequestAppointment);
        
        JPanel panel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        add(panel);
        
        JLabel lblDate = new JLabel(" Date:");
        lblDate.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblDate);
        
        final JComboBox comboBoxMonth = new JComboBox();
        comboBoxMonth.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxMonth.setModel(new DefaultComboBoxModel(new String[] {"Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}));
        comboBoxMonth.setSelectedIndex(0);
        panel.add(comboBoxMonth);
        
        final JComboBox comboBoxDay = new JComboBox();
        comboBoxDay.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxDay.setModel(new DefaultComboBoxModel(new String[] {"Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}));
        panel.add(comboBoxDay);
        
        final JComboBox comboBoxYear = new JComboBox(years);
        comboBoxYear.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(comboBoxYear);
        
        JLabel lblTime = new JLabel("     Time:");
        lblTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel.add(lblTime);
        
        final List<GregorianCalendar> calendars = new ArrayList<GregorianCalendar>();
        String[] visibleCalendars = new String[24];
        final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        for (int i = 0; i < 24; i++) {
            calendars.add(new GregorianCalendar(2011, Calendar.JANUARY, 1));
        }
        for (int i = 0; i < 24; i++) {
            calendars.get(i).add(Calendar.HOUR, 8 + (i / 2 ));
            calendars.get(i).add(Calendar.MINUTE, 30 * (i % 2));
            visibleCalendars[i] = sdf.format(calendars.get(i).getTime());
        }
        final JComboBox comboBoxTime = new JComboBox();
        comboBoxTime.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxTime.setModel(new DefaultComboBoxModel(visibleCalendars));
        panel.add(comboBoxTime);
        
        JPanel panel_1 = new JPanel();
        FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        add(panel_1);
        
        JLabel lblReasonForAppointment = new JLabel(" Reason for appointment:");
        lblReasonForAppointment.setHorizontalAlignment(SwingConstants.CENTER);
        lblReasonForAppointment.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_1.add(lblReasonForAppointment);
        
        final JComboBox comboBoxReason = new JComboBox();
        comboBoxReason.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxReason.setModel(new DefaultComboBoxModel(new String[] {"Select", "Physical Examination", "Injury", "Therapy", "Other (please specify)"}));
        panel_1.add(comboBoxReason);
        
        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
        flowLayout_2.setAlignment(FlowLayout.LEFT);
        add(panel_2);
        
        JLabel lblAdditionalNotesoptional = new JLabel(" Additional Notes (optional):");
        lblAdditionalNotesoptional.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_2.add(lblAdditionalNotesoptional);
        
        textField = new JTextField();
        textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_2.add(textField);
        textField.setColumns(15);
        
        JPanel panel_3 = new JPanel();
        FlowLayout flowLayout_3 = (FlowLayout) panel_3.getLayout();
        flowLayout_3.setAlignment(FlowLayout.LEFT);
        add(panel_3);
        
        JLabel lblDesiredDoctor = new JLabel(" Desired Doctor:");
        lblDesiredDoctor.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_3.add(lblDesiredDoctor);
        
        final JComboBox comboBoxDoctor = new JComboBox();
        panel_3.add(comboBoxDoctor);
        comboBoxDoctor.setModel(new DefaultComboBoxModel(doctorNames));
        comboBoxDoctor.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        JPanel panel_4 = new JPanel();
        add(panel_4);
        
        JButton btnRequestAppointment = new JButton("Request Appointment");
        btnRequestAppointment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String month = (String)comboBoxMonth.getSelectedItem();
                String day = (String)comboBoxDay.getSelectedItem();
                String year = (String)comboBoxYear.getSelectedItem();
                int timeIndex = comboBoxTime.getSelectedIndex();
                String reason = (String)comboBoxReason.getSelectedItem();
                String doctorName = (String)comboBoxDoctor.getSelectedItem();
                
                if (month.equals("Month")) {
                    JOptionPane.showMessageDialog(null, "Please select a month.");
                }
                else if (day.equals("Day")) {
                    JOptionPane.showMessageDialog(null, "Please select a day.");
                }
                else if (year.equals("Year")) {
                    JOptionPane.showMessageDialog(null, "Please select a year.");
                }
                else if (timeIndex == 0) {
                    JOptionPane.showMessageDialog(null, "Please select a time.");
                }
                else if (reason.equals("Select")) {
                    JOptionPane.showMessageDialog(null, "Please select a reason for the appointment.");
                }
                else if (doctorName.equals("Select")) {
                    JOptionPane.showMessageDialog(null, "Please select a desired doctor.");
                }
                else {
                    if (reason.equals("Other (please specify)")) {
                        reason = textField.getText();
                    }
                    GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year),
                            comboBoxMonth.getSelectedIndex() - 1, Integer.parseInt(day));
                    Doctor desiredDoctor = doctors.get(comboBoxDoctor.getSelectedIndex() - 1);
                    SystemDriver.createAppointment(0, 0, patient.getUserID(), 
                            false, patient, reason, desiredDoctor.getName(), 
                            sdf.format(calendars.get(timeIndex).getTime()), 
                            calendar.get(GregorianCalendar.YEAR) + "-" +
                            calendar.get(GregorianCalendar.MONTH) + "-" +
                            calendar.get(GregorianCalendar.DAY_OF_MONTH));
                    JOptionPane.showMessageDialog(null, "Appointment scheduled.");
                    window.gotoPanel("patientHome", patient);
                }
            }
        });
        panel_4.add(btnRequestAppointment);
        btnRequestAppointment.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnCancel = new JButton("Cancel");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("patientHome", patient);
            }
        });
        btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_4.add(btnCancel);
        
        JPanel panel_5 = new JPanel();
        add(panel_5);
        
        JPanel panel_6 = new JPanel();
        add(panel_6);

    }

}
